package club.baibin0558.jstudy.web.controller.business;

import club.baibin0558.jstudy.core.po.JStudyNode;
import club.baibin0558.jstudy.core.utils.ApiResult;
import club.baibin0558.jstudy.service.JStudyNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类型控制器
 *
 * @author 白彬
 */
@RestController
@RequestMapping("/java/study")
@Api("学习类型增删改查接口")
public class JStudyContriller {
    @Autowired
    private JStudyNodeService jStudyNodeService;

    /**
     * 根据上级code查询下级的数据
     *
     * @param parentCode
     * @return
     */
    @ApiOperation(value = "根据parentCode查询儿子类型", notes = "根据parentCode查询儿子类型")
    @ApiImplicitParam(name = "parentCode", value = "父类code", paramType = "path", required = true, dataType = "String")
    @GetMapping("/parent/{code}")
    public ApiResult selectList(@PathVariable("code") String parentCode) {
        List<JStudyNode> nodeList = jStudyNodeService.selectList(parentCode);
        return ApiResult.success(nodeList);
    }
    /**
     * 查询所有
     *
     * @return
     */
    @ApiOperation(value = "查询tree所有", notes = "查询tree所有")
    @GetMapping("/tree")
    public ApiResult selectList() {
        return ApiResult.success(jStudyNodeService.selectTree());
    }

    /**
     * 根据上级code查询下级的数据
     *
     * @param node 需要保存的数据
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存", notes = "保存")
    public ApiResult save(@RequestBody JStudyNode node) {
        return ApiResult.success(jStudyNodeService.save(node));
    }

    /**
     * 根据id删除数据
     *
     * @param id 需要删除的数据
     * @return
     */
    @PostMapping("/remove/{id}")
    @ApiOperation(value = "删除", notes = "删除")
    @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true, dataType = "INTEGER")
    public ApiResult remove(@PathVariable("id") Long id) {
        return ApiResult.success(jStudyNodeService.remove(id));
    }
}
